bitkeeper revision 1.1108.1.12 (40ffee27o1VPDwZVRgzbC8H1a0rrLQ)
authormjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Thu, 22 Jul 2004 16:41:11 +0000 (16:41 +0000)
committermjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Thu, 22 Jul 2004 16:41:11 +0000 (16:41 +0000)
Small fix in error handling.

tools/python/xen/xend/XendVnet.py
tools/python/xen/xend/server/SrvBase.py

index 213408e11105a25a2cd2270a48e9e1a3124b2346..83506b6c6c810fb1cfa38ef83da994a409258732 100644 (file)
@@ -3,8 +3,58 @@
 """Handler for vnet operations.
 """
 
+from xen.util import Brctl
+
 import sxp
 import XendDB
+from XendError import XendError
+from XendLogging import log
+
+def vnet_cmd(cmd):
+    out = None
+    try:
+        try:
+            out = file("/proc/vnet/policy", "wb")
+            sxp.show(cmd, out)
+        except IOError, ex:
+            raise XendError(str(ex))
+    finally:
+        if out: out.close()
+
+class XendVnetInfo:
+    
+    vifctl_ops = {'up': 'vif.add', 'down': 'vif.del'}
+    
+    def __init__(self, config):
+        self.config = config
+        self.id = sxp.child_value(config, 'id')
+        self.id = str(self.id)
+        self.bridge = sxp.child_value(config, 'bridge')
+        if not self.bridge:
+            self.bridge = "vnet%s" % self.id
+        self.vnetif = sxp.child_value(config, 'vnetif')
+        if not self.vnetif:
+            self.vnetif = "vnetif%s" % self.id
+
+    def sxpr(self):
+        return self.config
+
+    def configure(self):
+        log.info("Configuring vnet %s", self.id)
+        val = vnet_cmd(['vnet.add'] + sxp.children(self.config))
+        Brctl.bridge_create(self.bridge)
+        Brctl.vif_bridge_add({'bridge': self.bridge, 'vif': self.vnetif})
+        return val
+        
+    def delete(self):
+        log.info("Deleting vnet %s", self.id)
+        Brctl.vif_bridge_rem({'bridge': self.bridge, 'vif': self.vnetif})
+        Brctl.bridge_del(self.bridge)
+        return vnet_cmd(['vnet.del', self.id])
+
+    def vifctl(self, op, vif, vmac):
+        fn = self.vifctl_ops[op]
+        return vnet_cmd([fn, ['vif', vif], ['vmac', vmac]])
 
 class XendVnet:
     """Index of all vnets. Singleton.
@@ -16,49 +66,65 @@ class XendVnet:
         # Table of vnet info indexed by vnet id.
         self.vnet = {}
         self.db = XendDB.XendDB(self.dbpath)
-        self.vnet = self.db.fetchall("")
+        vnets = self.db.fetchall("")
+        for config in vnets.values():
+            info = XendVnetInfo(config)
+            self.vnet[info.id] = info
+            try:
+                info.configure()
+            except:
+                log.exception("Error configuring vnet")
+
+    def vnet_of_bridge(self, bridge):
+        """Get the vnet for a bridge (if any).
+
+        @param bridge: bridge name
+        @return vnet or None
+        """
+        for v in self.vnet.values():
+            if v.bridge == bridge:
+                return v
+        else:
+            return None
 
     def vnet_ls(self):
-        """List all vnets.
+        """List all vnet ids.
         """
         return self.vnet.keys()
 
     def vnets(self):
+        """List all vnets.
+        """
         return self.vnet.values()
 
     def vnet_get(self, id):
         """Get a vnet.
 
-        id     vnet id
+        @param id: vnet id
         """
+        id = str(id)
         return self.vnet.get(id)
 
-    def vnet_create(self, info):
+    def vnet_create(self, config):
         """Create a vnet.
 
-        info   config
-        """
-        self.vnet_configure(info)
-
-    def vnet_configure(self, info):
-        """Configure a vnet.
-        id     vnet id
-        info   config
+        @param config: config
         """
-        # Need to configure for real.
-        # Only sync if succeeded - otherwise need to back out.
+        info = XendVnetInfo(config)
         self.vnet[info.id] = info
-        self.db.save(info.id, info)
+        self.db.save(info.id, info.sxpr())
+        info.configure()
 
     def vnet_delete(self, id):
         """Delete a vnet.
 
-        id     vnet id
+        @param id: vnet id
         """
-        # Need to delete for real. What if fails?
-        if id in self.vnet:
+        info = self.vnet_get(id)
+        if info:
             del self.vnet[id]
             self.db.delete(id)
+            info.delete()
 
 def instance():
     global inst
index 048339e0b48461f6e33886122e6efae817cc27f1..6bc32b42bc54e7912e9e4e878c52d301e5f92e82 100644 (file)
@@ -161,8 +161,7 @@ class SrvBase(resource.Resource):
             sxp.show(['xend.err', str(err)], out=req)
         else:
             req.setHeader("Content-Type", "text/plain")
-            req.write('Error in ')
-            req.write(op)
+            req.write('Error ')
             req.write(': ')
             req.write(str(err))
         if dfr: